package com.lsu.app.tool;


import com.lsu.app.bean.UserTaste;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author hjj
 * @date 2023/10/22 16:36
 */
public class uploadUtil { //用来生成默认图片的工具类
    /**
     * 自定义生成默认图片
     * @param text
     * @param uploadPath
     * @param fileName
     * @return
     */
    public static R<String> defaultImgByString(String text,String uploadPath,String fileName){//根据上传文本，路径达到生成默认图片
        int imgWidth = 500;
        int imgHeight = 500;
        int len = text.length();
        // 创建图像
        BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        if (len > 10) {
            int size = imgWidth/(3+len/2);
            int x= (int)(1.5*size);
            String textu = text.substring(0,len/2);
            String textd= text.substring(len/2,len-1);
            // 设置随机字体和颜色
            Font font = new Font(Font.SANS_SERIF, Font.BOLD, size);
            g2d.setFont(font);
            g2d.setColor(Color.WHITE); // 设置字体颜色为白色
            //写入
            if(len%2==1){
                x+=(int)(size*0.7);
            }
            g2d.drawString(textu,x,imgHeight/2-size);
            g2d.drawString(textd,x,imgHeight/2+size);
        }else {
            int size = imgWidth/(3+len);
            int x= (int)(1.5*size);
            // 设置随机字体和颜色
            Font font = new Font(Font.SANS_SERIF, Font.BOLD, size);
            g2d.setFont(font);
            g2d.setColor(Color.WHITE); // 设置字体颜色为白色
            //写入
            if(len%2==1){
                x+=(int)(size*0.5);
            }
            g2d.drawString(text, x,imgHeight/2+size/2);
        }
        // 释放资源
        g2d.dispose();
        // 写入文件
        try {
            File output = new File(uploadPath+fileName+".jpg");//默认jpg文件
            ImageIO.write(image, "jpg", output);
            return R.success(fileName+".jpg");
        } catch (IOException e) {
            return R.err("存储图片异常");
        }
    }

    /**
     * 生成/上传 图片
     * @param file
     * @param uploadPath
     * @param fileName
     * @return
     */
    public static R<String> uploadMedia(MultipartFile file,String uploadPath, String fileName){
        if(file==null||!Objects.requireNonNull(file.getOriginalFilename()).contains(".")||file.isEmpty()){
            return defaultImgByString(fileName, uploadPath, fileName);
        }
        //文件后缀名
        String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        File orfile  = new File(uploadPath+fileName+suffix);
        if(orfile.exists()){
            if(!orfile.delete()) {//删除失败直接返回
                return R.err("上传失败");
            }
        }
        try{
            file.transferTo(orfile);
            return R.success(fileName+suffix);
        }catch (Exception e){
            return R.err("上传异常");
        }
    }

    /**
     * 删除文件
     * @param path
     * @return
     */
    public static R<String> deleteFile(String path){
        File orfile = new File(path);
        if(orfile.exists()){
            if (!orfile.delete()){
                return R.err("删除失败");
            }
        }else{
            return R.err("文件不存在");
        }
        return R.success("删除成功");
    }

}
